package com.linkage.educloud.ah.view.activity.big_picture;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;

import com.linkage.educloud.ah.R;
import com.linkage.educloud.ah.adapter.pic.BigpicAdapter;
import com.linkage.educloud.ah.bean.PicItem;
import com.linkage.educloud.ah.view.BasicTitleActivity;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * Created by Mr'Tang on 2018/9/19.
 */

public class BigPictureActivity extends BasicTitleActivity {
    private static final String EXTRAS_IMAGES = "extras_images";
    private static final String EXTRAS_POSITION = "extras_position";
    private static final String STATE_POSITION = "state_position";

    @BindView(R.id.activity_pic_viewpager)
    ViewPager activity_pic_viewpager;

    private ArrayList<PicItem> mImages;
    private BigpicAdapter mImagesAdapter;

    public static void launch(Context context, ArrayList<PicItem> images,
                             int position) {
        Intent intent = new Intent(context, BigPictureActivity.class);
        intent.putParcelableArrayListExtra(EXTRAS_IMAGES, images);
        intent.putExtra(EXTRAS_POSITION, position);
        context.startActivity(intent);
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_big_picture;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            finish();
            return;
        }
        mImages = extras.getParcelableArrayList(EXTRAS_IMAGES);
        int position = extras.getInt(EXTRAS_POSITION, 0);
        if (savedInstanceState != null) {
            position = savedInstanceState.getInt(STATE_POSITION);
        }
        activity_pic_viewpager
                .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageSelected(int arg0) {
                        setTitle((arg0 + 1) + "/" + mImages.size());
                    }

                    @Override
                    public void onPageScrolled(int arg0, float arg1, int arg2) {

                    }

                    @Override
                    public void onPageScrollStateChanged(int arg0) {

                    }
                });
        mImagesAdapter = new BigpicAdapter(BigPictureActivity.this, mImages);
        activity_pic_viewpager.setAdapter(mImagesAdapter);
        activity_pic_viewpager.setCurrentItem(position);
        setTitle((position + 1) + "/" + mImages.size());
    }
}
